package Query;

import Database.Document;
import java.util.Vector;
import Query.SearchEngine.*;

public class Dispatcher {
    
    Vector<IEngineAdapter> engineAdapters; 
            
    public Dispatcher()
    {
        this.engineAdapters = new Vector<IEngineAdapter>();
        engineAdapters.add(new GoogleEngineAdapter());
        engineAdapters.add(new YahooEngineAdapter());
        engineAdapters.add(new LiveSearchEngineAdapter());
    }
    
    public Vector<Vector<Document>> PerformQueries(String query, Vector<Priority> priorities)
    {
        Vector<Vector<Document>> result = this.getNewDocumentVector();
        
        for(int i = 0; i < this.engineAdapters.size(); i++)
        {
            int length = 0;
            for(int j=0; j<priorities.size(); j++)
                if(engineAdapters.get(i).getSearchEngine() == priorities.get(j).getSearchEngine())
                    length = priorities.get(j).getLength();
            result.add(engineAdapters.get(i).Query(query, length));
        }
        return result;
    }
    
    public Vector<Vector<Document>> PerformQueries(String query, int length)
    {
        Vector<Vector<Document>> result = this.getNewDocumentVector();
        for(int i = 0; i < this.engineAdapters.size(); i++)
        {
            Vector<Document> resultSet = engineAdapters.get(i).Query(query, length);
            if(resultSet == null) resultSet = new Vector<Document>();
            result.add(resultSet);
        }
        return result;
    }
    
    private Vector<Vector<Document>> getNewDocumentVector()
    {
        Vector<Vector<Document>> newDocumentVector = new Vector<Vector<Document>>();
        return newDocumentVector;
    }
}
